import { defineComponent, watch, shallowReactive, ref } from 'vue'
import * as echarts from 'echarts'
// 声明类型
const PropsType = {
  data: {
    type: Object,
    default: () => ({
      xAxis:[],
      series:[],
      unit:''
    }),
    require: true
  }
} as const

// 定义主体
export default defineComponent({
  props: PropsType,

  setup(props) {
    // 定义 ref
    const chartRef = ref()

    // 配置项
    let options = {}

    watch(
      () => props.data,
      (val: any) => {
        //
        options = {
          legend: {
            data:val.series.map(item=>item.name),//val.series.map(item=>item.name)
            textStyle: {
              align: 'right',
            },
            top: '2%',
            right: '2%',
          },

          tooltip: {
            trigger: 'axis',
            formatter: function (params) {
              let relVal = params[0].name
              for (let i = 0; i < params.length; i++) {
                relVal += '<br/>' + params[i].marker + params[i].seriesName + ' : ' + params[i].value
              }
              return relVal
            }
          },

          grid: {
            top: '24%',
            left: '8%',
            right: '8%',
            bottom: '10%',
          },
          xAxis: [
            {
              type: 'category',
              axisLine: {
                show: true,
              },
              splitArea: {
                // show: true,
                color: '#f00',
                lineStyle: {
                  color: '#f00',
                },
              },
              splitLine: {
                show: false,
              },
              axisLabel: {
                margin: 10,
              },
              boundaryGap: false,
              data: val.xAxis,
            },
          ],

          yAxis: [
            {
              type: 'value',
              name:`单位(${val.unit})`,
              // min: 0,
              // max: 140,
              nameTextStyle:{
                color:"#fff", 
              },
              splitNumber: 4,
              splitLine: {
                show: true,
                lineStyle: {
                  color: '#A1A7B3',
                  type: 'dashed'
                }
              },
              axisLine: {
                show: false,
              },
              axisLabel: {
                show: true,
                // margin: 20,
                formatter: '{value}',
              },
              axisTick: {
                show: false,
              },
            },
          ],
          series: val.series.map(item=>{
            return {
              name: item.name,
              type: 'line',
              smooth: true,
              symbolSize: 5,
              showSymbol: false,
              itemStyle: {
                normal: {
                  color: item.color,
                  lineStyle: {
                    color: item.color,
                    width: 2
                  },
                }
              },
              data: item.data,
            }
          })
        }
        // 手动触发更新
        if (chartRef.value) {
          // 通过初始化参数打入数据
          chartRef.value.initChart(options)
        }
      },
      {
        immediate: true,
        deep: true
      }
    )

    return () => {
      const height = "200px"
      const width = "448px"

      return <div>
        <echart ref={chartRef} class="mr-auto" options={options} height={height} width={width} />
      </div>
    }
  }
})

